<?php
// 本类由系统自动生成，仅供测试用途
class AdminuserAction extends BaseAction {

    public function _initialize(){
        parent::_initialize();
        //角色数组
        $this->assign('role', getAllRoles(true) );
        $this->assign('express', getExpress(true) );
    }
    public function index(){
        //$map['status'] = 1;
        $map = array();
        $kwd = $this->_post('keywords');
        if(isset($kwd) && !empty($kwd))
        {
            $map['username'] = $kwd;
            $map['realname'] = $kwd;
            $map['_logic'] = 'OR';
        }
        import('ORG.Util.Page');// 导入分页类
        $count      = M('Admin')->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page       = new Page($count);// 实例化分页类 传入总记录数
        $show       = $Page->show();// 分页显示输出

        $list = M('Admin')->where($map)->limit($Page->firstRow,$Page->listRows)->select();

        $query['kwd'] = $kwd;
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('query',$query);
        $this->display();
    }

    public function set()
    {
        if(IS_POST){
            $username = $this->_post('username');
            $realname = $this->_post('realname');
            $password = $this->_post('password');
            $role = $this->_post('role');
            $express = $this->_post('express');
            $id = $this->_post('id');
            $data = array();
            if($id){
                //修改
                $data['username'] = $username;
                if($password){
                    $data['password'] = md5($password);
                }
                if(3==$role){ //当角色是快递的时候要选择相应的快递公司
                    $data['attr'] = $express;
                }
                $data['realname'] = $realname;
                $data['roleid'] = $role;
                $re = M('Admin')->where(array('id'=>$id))->save($data);
                if ($re!==false) {
                    $this->_success('修改成功',U('Admin/Adminuser/index'));
                } else {
                    $this->_error('修改失败');
                }
            }else{
                //添加
                $isExist = D('Admin')->userExist($username);
                if($isExist){
                    $this->_error('该用户名已存在');
                }
                $data['username'] = $username;
                $data['password'] = md5($password);
                $data['realname'] = $realname;
                $data['roleid'] = $role;
                if(3==$role){ //当角色是快递的时候要选择相应的快递公司
                    $data['attr'] = $express;
                }
                $data['create_time'] = time();
                $instId = M('Admin')->add($data);
                if ($instId) {
                    $this->_success('添加成功',U('Admin/Adminuser/index'));
                } else {
                    $this->_error('添加失败');
                }
                
            }
        }else{

            $this->assign('act','add');
            $this->display();
        }
        
    }

    public function edit()
    {
        $id = (int)$this->_get('id',0);
        if($id)
        {
            $data = M('Admin')->where(array('id'=>$id))->find();
            if($data)
            {
                $this->assign('data',$data);
                $this->assign('act','edit');
                $this->display('set');
                exit;
            }

        }
        $this->_error('不存在该用户');
    }

    public function del()
    {
        $id = $this->_param('id',0);
        $re = false;
        if ($id) {
            $re = M('Admin')->where(array('id'=>$id))->delete();
        }
        
        if ($re) {
            $this->_success('删除成功');
        } else {
            $this->_error('删除失败');
        }
        
    }
}

?>